package physgin;

import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.*;
import java.awt.Container;

/**
 * A background implementation using the J3D library.
 */
public class J3dBackground implements Background<String, BranchGroup>
    {
        private BranchGroup objRoot;
        private TransformGroup objTrans;
        private Primitive plane;
        
        /**
         * Default constructor.
         */
        J3dBackground()
        {
            objRoot = new BranchGroup();
            objTrans = new TransformGroup();
            
            objRoot.setCapability(Group.ALLOW_CHILDREN_WRITE);
        }
        
        /**
         * Sets the background image to the file specified.
         *
         * @param   f   String of the file name.
         */
        public void setBackground(String f)
        { 
            Appearance ap = new Appearance();
            
            Color3f ambientColor  = new Color3f(1.0f, 1.0f, 1.0f);
            Color3f emissiveColor = new Color3f(0.0f, 0.0f, 0.0f);
            Color3f diffuseColor  = new Color3f(1.0f, 1.0f, 1.0f);
            Color3f specularColor = new Color3f(0.0f, 0.0f, 0.0f);
            ap.setMaterial(new Material(ambientColor, emissiveColor,
                                        diffuseColor, specularColor, 50f));
            
            Texture texture;
            texture = J3dWorld.TextureCache.loadResource(f);
            
            TextureAttributes texAttr = new TextureAttributes();
            texAttr.setTextureMode(TextureAttributes.COMBINE);
            ap.setTexture(texture);
            ap.setTextureAttributes(texAttr);
            
            int ps = Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS; 
            
            plane = new Box (5f, 7f, 1, ps, ap); //these values temporary, make better soon
            
            createSceneGraph();
        }
        
        
        private void createSceneGraph()
        {        
            Transform3D initialPos = new Transform3D();
            initialPos.setTranslation(new Vector3d(0f,3f,-10f));
            objTrans.setTransform(initialPos);
            
            objTrans.addChild(plane);
            objRoot.addChild(objTrans);
        }
        
        /**
         * Returns the background as a BranchGroup, for use by the J3D libraries.
         *
         * @return  BranchGroup the backround displayable as a BranchGroup.
         * @see     javax.media.j3d.BranchGroup
         */
        public BranchGroup getObject()
        {
            return objRoot;
        }
    }
